﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditUserFrm.aspx.cs" Inherits="WebApp.EditUserFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>编辑用户信息</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="js/Jquery.Query.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            initParams();
        });

        function initParams() {
            var userId = $.query.get('UserId');
            var orderId = $.query.get('OrderId');
            $('#orderId').val(orderId);
            $('#editId').val(userId);
            initData();
            bindUser();
            if (userId) {
                $("#eCode").attr({ readonly: 'true' });
                $("#ePwd").attr({ readonly: 'true' });
            }
        }

        function bindUser() {
            var id = $('#editId').val();
            if (id) {
                WebApp.EditUserFrm.GetUser(id, function callback(p) {
                    if (p.error) {
                        $.messager.alert('异常消息', '获取数据异常,' + p.error.Message, 'error');
                        return;
                    }

                    var res = p.value;
                    if (res) {
                        var data = res.Data;
                        var msg = res.Msg;
                        if (msg)
                            $.messager.alert('信息获取异常', msg, 'error');
                        else {
                            if (data) {
                                $('#eCode').val(data.code);
                                $('#eUserName').val(data.userName);
                                $('#ePost').val(data.post);
                                $('#eMobile').val(data.mobile);
                                $('#eTel').val(data.tel);
                                $('#ePwd').val(data.pwd);
                                $('#eRole').combobox('setValue', data.role);
                                $('#eSex').val(data.sex);
                                $('#eLeader').combogrid('setValue', data.leader);
                            }
                        }
                    }
                });
            }
        }

        function initData() {
            $.getJSON("JSONHandler.ashx?DataType=SysRoleType", function(result) {
                $('#eRole').combobox('loadData', result);
            });

            $.getJSON("JSONHandler.ashx?DataType=User", function(result) {
                $('#eLeader').combogrid("grid").datagrid("loadData", result);
            });
        }

        function resetFrm() {
            $('#fm').form('clear');
            $('#editId').val('');
            $("#eCode").removeAttr("readonly");
            $("#ePwd").removeAttr("readonly");
        }

        function addUser() {
            resetFrm();
        }

        function saveUser() {
            var validate = $('#fm').form('validate');
            if (!validate)
                return;

            var id = $('#editId').val();
            var code = $('#eCode').val();
            var userName = $('#eUserName').val();
            var post = $('#ePost').val();
            var mobile = $('#eMobile').val();
            var tel = $('#eTel').val();
            var pwd = $('#ePwd').val();
            var role = $('#eRole').combobox('getValue');
            var sex = $('#eSex').val();
            var leader = $('#eLeader').combogrid('getValue');

            WebApp.EditUserFrm.SaveUser(id, code, userName, post, mobile, tel, pwd, role, sex, leader, function callback(p) {
                if (p.error) {
                    $.messager.alert('异常消息', '提交保存异常,' + p.error.Message, 'error');
                    return;
                }

                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('信息获取异常', msg, 'error');
                    else {
                        refreshParent();
                        if (id) {
                            if (data > 0) {
                                $.messager.alert('消息', '信息已更新!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('信息更新异常', msg, 'error');
                        }
                        else {
                            if (data > 0) {
                                $.messager.alert('消息', '信息已创建!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('信息创建异常', msg, 'error');
                        }
                    }
                }
            });
        }

        function deleteUser() {
            $.messager.confirm('提示', '确认删除该项吗?', function(r) {
                if (r) {
                    var id = $('#editId').val();
                    WebApp.EditUserFrm.DeleteUser(id, function callback(p) {

                        if (p.error) {
                            $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                            return;
                        }
                        var res = p.value;
                        if (res) {
                            var data = res.Data;
                            var msg = res.Msg;
                            if (msg)
                                $.messager.alert('删除异常', msg, 'error');
                            else {
                                if (data == true) {
                                    $.messager.alert('消息', '信息已删除!', 'info');
                                    refreshParent();
                                }
                                else
                                    $.messager.alert('删除失败', msg, 'error');
                            }
                        }
                    });
                }
            });
        }

        function refreshParent() {
            window.parent.doSearch();
        }
    </script>

</head>
<body>
    <form id="Form1" method="post" runat="server">
    </form>
    <div>
        <form id="fm">
        <div style="padding: 5px; border: 1px solid #ddd">
            <a href="#" id="btnAdd" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="addUser()">
                新增</a> <a href="#" id="btnSave" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'"
                    onclick="saveUser()">保存</a> <a href="#" id="btnDelete" class="easyui-linkbutton"
                        data-options="plain:true,iconCls:'icon-remove'" onclick="deleteUser()">删除</a>
        </div>
        <div class="easyui-panel" title="用户资料" style="width: 500px; padding: 5px;">
            <table>
                <tr>
                    <td style="text-align: left">
                        帐号：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eCode" style="width: 150px" class="easyui-validatebox" data-options="required:true" />
                    </td>
                    <td style="text-align: left">
                        名称：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eUserName" style="width: 150px" class="easyui-validatebox"
                            data-options="required:true" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        角色：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eRole" data-options="valueField:'Value',textField:'Name'"
                            style="width: 150px;" required="true">
                    </td>
                    <td style="text-align: left">
                        性别：
                    </td>
                    <td style="text-align: left">
                        <select id="eSex">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        手机：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eMobile" style="width: 150px" />
                    </td>
                    <td style="text-align: left">
                        电话：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eTel" style="width: 150px" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        密码：
                    </td>
                    <td style="text-align: left">
                        <input type="password" id="ePwd" style="width: 150px" />
                    </td>
                    <td style="text-align: left">
                        上级：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combogrid" id="eLeader" style="width: 150px" data-options="  
            panelWidth: 150,  
            idField: 'id',  
            textField: 'userName',
            columns: [[{field:'userName',title:'名称',width:150}               
            ]],  
            fitColumns: true  
        ">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        职务：
                    </td>
                    <td style="text-align: left" colspan="3">
                        <input type="text" id="ePost" style="width: 150px" />
                    </td>
                </tr>
            </table>
        </div>
        <input id="editId" type="hidden" />
        </form>
        <input id="orderId" type="hidden" />
    </div>
</body>
</html>
